//////////////////////////////////////////////////////////////////////////////////////////////////
// 
// File: d3dUtility.h
// 
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 
//
// Desc: Provides utility functions for simplifying common tasks.
//          
//////////////////////////////////////////////////////////////////////////////////////////////////
/****************************************
Revision History

1.02 2008-05-17 Eugene Melnichek
Removed lpSprite parameter from InitD3D and added hWnd

1.01 2008-03-09 Eugene Melnichek
Included lpSprite parameter in InitD3D()

1.00 Roman Hwang
Initial revision
****************************************/
#ifndef __d3dUtilityH__
#define __d3dUtilityH__

#include <d3dx9.h>
#include <d3dx9core.h>
#include <string>

namespace d3d
{
	bool InitD3D(
		HINSTANCE hInstance,		// [in] Application instance.
		WNDPROC lpfnWndProc,		// [in] Pointer to the windows procedure to handle messages
		int width, int height,		// [in] Backbuffer dimensions.
		bool windowed,				// [in] Windowed (true)or full screen (false).
		D3DDEVTYPE deviceType,		// [in] HAL or REF
		IDirect3DDevice9** device,	// [out]The created device.
		HWND & hWnd					// [out]Handle to created main window
		);

	int EnterMsgLoop( 
		bool (*ptr_display)(float timeDelta));

	/*LRESULT CALLBACK WndProc(
		HWND hwnd,
		UINT msg, 
		WPARAM wParam,
		LPARAM lParam);*/

	const D3DXCOLOR     WHITE(D3DCOLOR_XRGB(255, 255, 255));
    const D3DXCOLOR     BLACK(D3DCOLOR_XRGB(  0,   0,   0));
    const D3DXCOLOR       RED(D3DCOLOR_XRGB(255,   0,   0));
    const D3DXCOLOR     GREEN(D3DCOLOR_XRGB(  0, 255,   0));
    const D3DXCOLOR      BLUE(D3DCOLOR_XRGB(  0,   0, 255));
    const D3DXCOLOR    YELLOW(D3DCOLOR_XRGB(255, 255,   0));
    const D3DXCOLOR      CYAN(D3DCOLOR_XRGB(  0, 255, 255));
    const D3DXCOLOR   MAGENTA(D3DCOLOR_XRGB(255,   0, 255));
	const D3DXCOLOR		 GREY(D3DCOLOR_XRGB( 75,  75,  75));

	D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p);

	const D3DMATERIAL9 BLACK_MTRL  = InitMtrl(BLACK, BLACK, BLACK, GREY, 2.0f);
	const D3DMATERIAL9 WHITE_MTRL  = InitMtrl(WHITE, WHITE, WHITE, BLACK, 2.0f);
	const D3DMATERIAL9 RED_MTRL    = InitMtrl(RED, RED, RED, BLACK, 2.0f);
	const D3DMATERIAL9 GREEN_MTRL  = InitMtrl(GREEN, GREEN, GREEN, BLACK, 2.0f);
	const D3DMATERIAL9 BLUE_MTRL   = InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);
	const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 2.0f);

	struct Vertex
	{
		Vertex() {}
		Vertex(float x, float y, float z,
			   float nx, float ny, float nz,
			   float u, float v)
			   :_x(x), _y(y), _z(z),
				_nx(nx), _ny(ny), _nz(nz),
				_u(u), _v(v)
		{
		
		}

		float _x, _y, _z;
		float _nx, _ny, _nz;
		float _u, _v;

		const static DWORD FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
	};

	

	D3DMATERIAL9 CreateMaterial( 
						D3DXCOLOR diffuse, 
						D3DXCOLOR ambient, 
						D3DXCOLOR specular,
						D3DXCOLOR emissive,
						float power);

	D3DLIGHT9 CreatePointLight( D3DXVECTOR3* position, D3DXCOLOR* color);

	D3DLIGHT9 CreateDirectionalLight( D3DXVECTOR3* direction, D3DXCOLOR* color);

	template<class T> void Release(T t)
	{
		if( t )
		{
			t->Release();
			t = 0;
		}
	}
		
	template<class T> void Delete(T t)
	{
		if( t )
		{
			delete t;
			t = 0;
		}
	}
}

#endif // __d3dUtilityH__